﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
using System.Data;
using System.IO;
using System.Drawing;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Windows;

public partial class SummarizeFeedbackUI : System.Web.UI.Page
{
    private OdbcConnection myConn;

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((String)Session["Username"] == null || (String)Session["Username"] != "admin")
        {
            //Response.End();
            Response.Redirect("~/LoginAdmin.aspx");
        }

        if (Page.IsPostBack == false)
        {

            //myConn = new OdbcConnection("Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=feedback;User=root;Password=mysql;");
            //myConn.Open();

            DateTime curr_date;
            curr_date = DateTime.Today;
            String dt_sql;
            dt_sql = curr_date.ToString("yyyy-MM-dd");
            String sql = "Select Course_ID, Course_Name, Feedback_ID from course_feedback where Summarization_Date<='" + dt_sql + "'";
            DbUtilities dbutil = new DbUtilities(sql);
            //string sql = "INSERT INTO Course_Feedback VALUES('1','" + courseCodeTextbox.Text + "',2,'third','" + courseInstructorTextbox.Text + "','" + courseCoInstructorTextbox.Text + "','" + taTextbox.Text + "','" + courseInstructorEmailTextbox.Text + "','" + courseCoInstructorEmailTextbox.Text + "','" + taEmailTextbox.Text + "','" + startDate.ToString("yyyy-MM-dd") + "','" + summDate.ToString("yyyy-MM-dd") + "')";
            //String sql = "Select Course_ID, Course_Name, Feedback_ID from course_feedback";
            //OdbcCommand cmd = new OdbcCommand(sql, myConn);
            //OdbcDataAdapter odbcAdapter = new OdbcDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            dataSet = dbutil.retrieveInfo();
            //odbcAdapter.Fill(dataSet);
            //myConn.Close();


            for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
            {
                dataSet.Tables[0].Rows[i][0] = " " + (String)dataSet.Tables[0].Rows[i].ItemArray[0] + " " + (String)dataSet.Tables[0].Rows[i].ItemArray[1];
            }

            //DeleteRadioButtonList = new RadioButtonList();

            SummarizeButtonList.DataSource = dataSet;
            SummarizeButtonList.DataTextField = "Course_ID";
            SummarizeButtonList.DataValueField = "Feedback_ID";
            SummarizeButtonList.DataBind();
            //DeleteRadioButtonList.Enabled = true;
        }
    }

    private void radioButtonListUpdate()
    {

        //myConn = new OdbcConnection("Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=feedback;User=root;Password=mysql;");
        //myConn.Open();

        DateTime curr_date;
        curr_date = DateTime.Today;
        String dt_sql;
        dt_sql = curr_date.ToString("yyyy-MM-dd");
        String sql = "Select Course_ID, Course_Name, Feedback_ID from course_feedback where Summarization_Date<='" + dt_sql + "'";
        DbUtilities dbutil = new DbUtilities(sql);
        DataSet dataSet = new DataSet();
        dataSet = dbutil.retrieveInfo();

        //string sql = "INSERT INTO Course_Feedback VALUES('1','" + courseCodeTextbox.Text + "',2,'third','" + courseInstructorTextbox.Text + "','" + courseCoInstructorTextbox.Text + "','" + taTextbox.Text + "','" + courseInstructorEmailTextbox.Text + "','" + courseCoInstructorEmailTextbox.Text + "','" + taEmailTextbox.Text + "','" + startDate.ToString("yyyy-MM-dd") + "','" + summDate.ToString("yyyy-MM-dd") + "')";
        //String sql = "Select Course_ID, Course_Name, Feedback_ID from course_feedback";
        //OdbcCommand cmd = new OdbcCommand(sql, myConn);
        //OdbcDataAdapter odbcAdapter = new OdbcDataAdapter(cmd);
        //DataSet dataSet = new DataSet();
        //odbcAdapter.Fill(dataSet);
        //myConn.Close();

        //DeleteRadioButtonList = new RadioButtonList();
        for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
        {
            dataSet.Tables[0].Rows[i][0] = " " + (String)dataSet.Tables[0].Rows[i].ItemArray[0] + " " + (String)dataSet.Tables[0].Rows[i].ItemArray[1];
        }

        SummarizeButtonList.DataSource = dataSet;
        SummarizeButtonList.DataTextField = "Course_ID";
        SummarizeButtonList.DataValueField = "Feedback_ID";
        SummarizeButtonList.DataBind();
    }


    protected void initiateSummarizeFeedback(object sender, EventArgs e)
    {
        String toSummarize = SummarizeButtonList.SelectedItem.Value;
        int index = SummarizeButtonList.SelectedIndex;

        if (Page.IsPostBack)
        {
            FeedbackResult feedbackresult = new FeedbackResult(this.Request.PhysicalApplicationPath);

            String courseID = SummarizeButtonList.SelectedItem.Text;
            String feedbackID = SummarizeButtonList.SelectedItem.Value;

            DataSet dataSet = new DataSet();
            dataSet = feedbackresult.getAllResponse(courseID, feedbackID);
            feedbackresult.calculateResult(dataSet, courseID, feedbackID);
            feedbackresult.revealResult();

            radioButtonListUpdate();

        }

    }


}